package net.zoneland.greenleaf.common.worktimehelper;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.zoneland.greenleaf.common.date.DateOperation;
import net.zoneland.greenleaf.system.entity.Sys_holidayconf;

import org.apache.log4j.Logger;

/**
 * 节假日辅助计算类
 * 主要功能： 判断一个日期是否为节假日
 * 成员：
 * holidayList<Holiday>  节假日列表： 用户定义的节假日，用与国家法定假日或者调休
 * workdayList<Workday>  工作日列表： 用户自定义的工作日，用与调休
 * @作者 liyi
 */
public class HolidayManager {
	
	public Logger logger = Logger.getLogger( HolidayManager.class );
	/**
	 * holidayList<Holiday>  节假日列表： 用户定义的节假日，用与国家法定假日或者调休
	 */
	private List<Sys_holidayconf> holidayList = null;
	
	/**
	 * workdayList<Workday>  工作日列表： 用户自定义的工作日，用与调休
	 */
	private List<Sys_holidayconf> workdayList = null;
	
	public HolidayManager(List<Sys_holidayconf> _holidayList, List<Sys_holidayconf> _workdayList ){
		holidayList = _holidayList;
		workdayList = _workdayList;
	}
	
	/**
	 * 判断一个日期是否为节假日
	 * 参数可以是 "2013-05-01" 也可以是 "2013-05-01 00：00：00"
	 * @param dateString
	 * @return true|false
	 */
	public boolean isHoliday(String dateString){
		Date date = null;
		try {
			if(dateString.length()>10){
				date = DateFormatUtil.FORMAT.parse(dateString);
			}else{
				date = DateFormatUtil.FORMAT_DATE.parse(dateString);
			}			
		} catch (ParseException e) {
			logger.error("系统异常", e);
		}		
		return isHoliday(date);
	}
	
	/**
	 * 判断一个日期是否为节假日
	 * 
	 * @param date
	 * @return true|false
	 */
	public boolean isHoliday(Date date){
		DateOperation dateOperation = new DateOperation();
		if(date != null){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			//看看是否已经注册为工作日 ， 工作日优先
			if(this.workdayList != null && this.workdayList.size()>0){
				Sys_holidayconf workday = null;
				Date startdate = null;				
				for(int i=0;i<this.workdayList.size();i++){
					workday = (Sys_holidayconf)workdayList.get(i);
					try {
						startdate = dateOperation.getDateFromString(workday.getStarttime(), "yyyy-MM-dd HH:mm:ss");
					} catch (Exception e) {
						logger.error("系统异常", e);
					}
					if(dateOperation.getDate(date).equals(dateOperation.getDate(startdate))){
						return false;
					}						
				}
			}			
			
			//看看是否已经注册为节假日 
			Date holiday_date = null;
			if(this.holidayList != null && this.holidayList.size()>0){
				Sys_holidayconf holiday = null;
				for(int i=0;i<this.holidayList.size();i++){
					holiday = (Sys_holidayconf)holidayList.get(i);
					if(holiday!=null && holiday.getStarttime()!=null && holiday.getStarttime().length()>10){
						try {
							holiday_date = dateOperation.getDateFromString(holiday.getStarttime(), "yyyy-MM-dd HH:mm:ss");
						} catch (Exception e) {
							logger.error("系统异常", e);
						}
					}else{
						try {
							holiday_date = dateOperation.getDateFromString(holiday.getStarttime().split(" ")[0], "yyyy-MM-dd");
						} catch (Exception e) {
							logger.error("系统异常", e);
						}
					}
					if(dateOperation.getDate(date).equals(dateOperation.getDate(holiday_date))){
						return true;
					}						
				}
			}
			
			//如果既未注册为节假日，也未注册为工作日，则按正常周末休息，看看是否周末
			//判断是否周末，周末为周六周日，DAY_OF_WEEK=7或者DAY_OF_WEEK=8
			if(calendar.get(Calendar.DAY_OF_WEEK)==7){//星期六
				//System.out.println("星期六");
				return true;
			}else if(calendar.get(Calendar.DAY_OF_WEEK)==1){//星期日
				//System.out.println("星期日");
				return true;
			}
			//System.out.println(calendar.get(Calendar.DAY_OF_WEEK));
		}
		
		return false;
	}
	
	public List<Sys_holidayconf> getHolidayList() {
		return holidayList;
	}
	public void setHolidayList(List<Sys_holidayconf> holidayList) {
		this.holidayList = holidayList;
	}
	public List<Sys_holidayconf> getWorkdayList() {
		return workdayList;
	}
	public void setWorkdayList(List<Sys_holidayconf> workdayList) {
		this.workdayList = workdayList;
	}
	
}
